package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysSocialUserBo;
import org.dromara.system.domain.vo.SysSocialUserVo;
import org.dromara.system.domain.SysSocialUser;
import org.dromara.system.mapper.SysSocialUserMapper;
import org.dromara.system.service.ISysSocialUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 第三方登录Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-23
 */
@RequiredArgsConstructor
@Service
public class SysSocialUserServiceImpl implements ISysSocialUserService {

    private final SysSocialUserMapper baseMapper;

    /**
     * 查询第三方登录
     */
    @Override
    public SysSocialUserVo queryById(String openid){
        return baseMapper.selectVoById(openid);
    }

    /**
     * 查询第三方登录列表
     */
    @Override
    public TableDataInfo<SysSocialUserVo> queryPageList(SysSocialUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSocialUser> lqw = buildQueryWrapper(bo);
        Page<SysSocialUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询第三方登录列表
     */
    @Override
    public List<SysSocialUserVo> queryList(SysSocialUserBo bo) {
        LambdaQueryWrapper<SysSocialUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSocialUser> buildQueryWrapper(SysSocialUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSocialUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()), SysSocialUser::getOpenid, bo.getOpenid());
        lqw.eq(StringUtils.isNotBlank(bo.getUser_id()), SysSocialUser::getUserId, bo.getUser_id());
        lqw.eq(bo.getUser_type() != null, SysSocialUser::getUserType, bo.getUser_type());
        lqw.like(StringUtils.isNotBlank(bo.getUser_name()), SysSocialUser::getUserName, bo.getUser_name());
        return lqw;
    }

    /**
     * 新增第三方登录
     */
    @Override
    public Boolean insertByBo(SysSocialUserBo bo) {
        SysSocialUser add = MapstructUtils.convert(bo, SysSocialUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOpenid(add.getOpenid());
        }
        return flag;
    }

    /**
     * 修改第三方登录
     */
    @Override
    public Boolean updateByBo(SysSocialUserBo bo) {
        SysSocialUser update = MapstructUtils.convert(bo, SysSocialUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysSocialUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除第三方登录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
